<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $bbs = new \app\admin\model\bbs\Banner;
        $bbsBanner = $bbs->order('weigh desc,id desc')->select();

        $model = new \app\admin\model\bbs\Contents;
        $res['bbs'] = $model->order('weigh DESC,id DESC')->limit(2)->select();
        $model = new \app\admin\model\video\Content;
        $res['video'] = $model->order('weigh DESC,id DESC')->limit(1)->select();

        $model = new \app\admin\model\entry\Article;
        $res['entry'] = $model->order('weigh DESC,id DESC')->limit(2)->select();

        $this->view->assign('bbsBanner', $bbsBanner);
        $this->view->assign('res', $res);
        return $this->view->fetch();
    }

    public function message()
    {
        if ($this->request->isAjax()) {
            $data['name']       = $this->request->post('name');
            $data['tel']        = $this->request->post('tel');
            $data['content']    = $this->request->post('content');
            $model = new \app\admin\model\Message;
            $model->save($data);
        }
    }

    public function search($action = 2, $text = '')
    {
        switch ($action) {
            case 1:
                $model = new \app\admin\model\entry\Article;
                $list = $model->where('title', 'like', '%'.$text.'%')->paginate(10);
                break;
            case 2:
                $model = new \app\admin\model\bbs\Contents;
                $list = $model->with('models')->where('title', 'like', '%'.$text.'%')->paginate(10);
                break;
            case 3:
                $model = new \app\admin\model\video\Content;
                $list = $model->where('title', 'like', '%'.$text.'%')->paginate(10);
                break;
        }
        $this->view->assign('list', $list);
        $this->view->assign('pages', $list->render());
        $this->view->assign('action', $action);
        $this->view->assign('text', $text);
        return $this->view->fetch();
    }

    public function zonglun()
    {
        return $this->view->fetch();
    }

    public function entry()
    {
        return $this->view->fetch();
    }

    public function entryArticle($id = null)
    {
        $model = new \app\admin\model\entry\Article;
        $list = $model->where('classify_id', $id)->field('id,title,weigh')->order('weigh desc,id desc')->paginate(10);
        $this->view->assign('list', $list);
        $this->view->assign('pages', $list->render());
        return $this->view->fetch();
    }

    public function entryDetail($ids = null)
    {
        $model = new \app\admin\model\entry\Article;
        $row = $model->get($ids);
        $row->setInc('hits');
        $this->view->assign('row', $row);
        $tpl = '';
        if ($row['type'] == 2) {
            $tpl = 'digitalslides_detail';
            $row['files'] = json_decode($row['files'], true);
        }
        return $this->view->fetch($tpl);
    }

    public function agreement()
    {
        return $this->view->fetch();
    }

}
